package cn.iocoder.yudao.module.system.controller.admin.deptarea;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.deptarea.vo.DeptAreaPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.deptarea.vo.DeptAreaRespVO;
import cn.iocoder.yudao.module.system.controller.admin.deptarea.vo.DeptAreaSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.deptarea.DeptAreaDO;
import cn.iocoder.yudao.module.system.service.deptarea.DeptAreaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 部门区域关系")
@RestController
@RequestMapping("/system/dept-area")
@Validated
public class DeptAreaController {

    @Resource
    private DeptAreaService deptAreaService;

    @PostMapping("/create")
    @Operation(summary = "创建部门区域关系")
    @PreAuthorize("@ss.hasPermission('system:dept-area:create')")
    public CommonResult<Long> createDeptArea(@Valid @RequestBody DeptAreaSaveReqVO createReqVO) {
        return success(deptAreaService.createDeptArea(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新部门区域关系")
    @PreAuthorize("@ss.hasPermission('system:dept-area:update')")
    public CommonResult<Boolean> updateDeptArea(@Valid @RequestBody DeptAreaSaveReqVO updateReqVO) {
        deptAreaService.updateDeptArea(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除部门区域关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:dept-area:delete')")
    public CommonResult<Boolean> deleteDeptArea(@RequestParam("id") Long id) {
        deptAreaService.deleteDeptArea(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得部门区域关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:dept-area:query')")
    public CommonResult<DeptAreaRespVO> getDeptArea(@RequestParam("id") Long id) {
        DeptAreaDO deptArea = deptAreaService.getDeptArea(id);
        return success(BeanUtils.toBean(deptArea, DeptAreaRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得部门区域关系分页")
    @PreAuthorize("@ss.hasPermission('system:dept-area:query')")
    public CommonResult<PageResult<DeptAreaRespVO>> getDeptAreaPage(@Valid DeptAreaPageReqVO pageReqVO) {
        PageResult<DeptAreaDO> pageResult = deptAreaService.getDeptAreaPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeptAreaRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出部门区域关系 Excel")
    @PreAuthorize("@ss.hasPermission('system:dept-area:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeptAreaExcel(@Valid DeptAreaPageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeptAreaDO> list = deptAreaService.getDeptAreaPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "部门区域关系.xls", "数据", DeptAreaRespVO.class,
                BeanUtils.toBean(list, DeptAreaRespVO.class));
    }

}